package com.suypower.carManage.comm;

import java.util.*;
import java.text.*;


public class pdate {
    public pdate() {
    }

    /**
        ȡ�ø����ڵ���

        @param mdate
     - ����
        @return int
        @since 2002-10-28
        @roseuid 3DC20339033F
     */
    public static int GetYear(Date mdate) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy");
        String syear = sd.format(mdate);
        return Integer.parseInt(syear);
    }

    /**
        ȡ�ø��ַ���ꡢ�¡���
     */
    public static String GetStringTime(String stime) {
        StringBuffer s = new StringBuffer();
        s.append(String.valueOf(pdate.GetYear(stime)) + "-" +
                 String.valueOf(pdate.GetMonth(stime)) + "-" +
                 String.valueOf(pdate.GetDay(stime)));
        return s.toString();
    }


    /**
        ȡ�ø����ڵ���

        @param mdate - ����
        @return int
        @roseuid 3DC203810284
     */
    public static int GetDay(Date mdate) {

        SimpleDateFormat sd = new SimpleDateFormat("dd");
        String sday = sd.format(mdate);
        return Integer.parseInt(sday);
    }

    /**
        ȡ�ø��ַ����

        @param mdate
        @return int
        @roseuid 3DC203CD0234
     */
    public static int GetMonth(String mdate) {
        String smon1, smon2;
        StringBuffer smon = new StringBuffer();
        smon1 = mdate.substring(5, 6);
        smon2 = mdate.substring(6, 7);
        if (smon2.equals("-")) {
            smon.append(smon1);
        } else {
            smon.append(smon1);
            smon.append(smon2);
        }
        return Integer.parseInt(smon.toString());
    }

    /**
        ȡ�õ������

        @return int
        @roseuid 3DC2091701F4
     */
    public static int GetDay() {
        Date today = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("dd");
        String sday = sd.format(today);
        return Integer.parseInt(sday);
    }

    /**
        ȡ�ø��ַ����

        @param mdate - �������ַ��硰2002-10-11�� "2002-9-2"
        @return int
        @roseuid 3DC203F6020A
     */
    public static int GetYear(String mdate) {
        String syear = mdate.substring(0, 4);
        return Integer.parseInt(syear);
    }

    /**
        ȡ�õ������ڵ��·�

        @return int
        @roseuid 3DC209510284
     */
    public static int GetMonth() {
        Date today = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("MM");
        String smon = sd.format(today);
        return Integer.parseInt(smon);
    }

    /**
        ȡ�ø��ַ�ļ���

        @param mdate
        @return int
        @roseuid 3DC203CD0234
     */
    public static String GetQuarter() {
        Date today = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("MM");
        String smon = sd.format(today);
        int month = Integer.parseInt(smon);
        String quarter = "";
        if (month == 1) {
            quarter = "1";
        }
        if (month == 2) {
            quarter = "1";
        }
        if (month == 3) {
            quarter = "1";
        }
        if (month == 4) {
            quarter = "2";
        }
        if (month == 5) {
            quarter = "2";
        }
        if (month == 6) {
            quarter = "2";
        }
        if (month == 7) {
            quarter = "3";
        }
        if (month == 8) {
            quarter = "3";
        }
        if (month == 9) {
            quarter = "3";
        }
        if (month == 10) {
            quarter = "4";
        }
        if (month == 11) {
            quarter = "4";
        }
        if (month == 12) {
            quarter = "4";
        }
        return quarter;

    }

    /**
        ��õ�ǰ��ʱ

        @return int
        @roseuid 3DC20A58008D
     */
    public static int GetHour() {
        Calendar now_Time = Calendar.getInstance();
        int hour = now_Time.get(Calendar.HOUR_OF_DAY);
        return hour;
    }

    /**
        ȡ�ø��ַ����

        @param mdate - �������ַ��硰2002-10-11�� "2002-9-2"
        @return int
        @roseuid 3DC204350265
     */
    public static int GetDay(String mdate) {
        int i = 0, j;
        int l = mdate.length();
        String s = "", sday1, sday2;
        StringBuffer sday = new StringBuffer();
        for (j = 0; j < 2; j++, s = "") {
            while (!(s.equals("-"))) {
                s = mdate.substring(i, i + 1);
                i++;
            }
        }
        sday1 = mdate.substring(i, i + 1);
        sday.append(sday1);
        if (!(i == l - 1)) {
            sday2 = mdate.substring(i + 1, i + 2);
            sday.append(sday2);
        }
        return Integer.parseInt(sday.toString());
    }


    /**
        ȡ�ø����ڵ���

        @param mdate - �������ַ��硰2002-10-11�� "2002-9-2"
        @return int
        @roseuid 3DC20463024D
     */
    public static int GetMonth(Date mdate) {
        SimpleDateFormat sm = new SimpleDateFormat("MM");
        String smon = sm.format(mdate);
        return Integer.parseInt(smon);
    }

    /**
        ת��Ϊ�����������ַ�2002-10-11��

        @param mdate
        @return String
        @roseuid 3DC204CC024E
     */
    public static String cnDate(Date mdate) {
        String syear = String.valueOf(GetYear(mdate));
        String smon = String.valueOf(GetMonth(mdate));
        String sday = String.valueOf(GetDay(mdate));
        String cndate = syear + "��" + smon + "��" + sday + "��";
        return cndate;
    }

    /**
        ��õ�ǰ�ķ�

        @return int
        @roseuid 3DC20A8E02E4
     */
    public static int GetMinute() {
        Calendar now_Time = Calendar.getInstance();
        int minute = now_Time.get(Calendar.MINUTE);
        return minute;
    }

    /**
        �ж��Ƿ��������ͣ�������������ͣ�����true
        else ����false

        @param mdate - �ַ�
        @return boolean
        @roseuid 3DC205500226
     */
    public static boolean IsDate(String mdate) {
        String s;
        int syear_int, smon_int, sday_int;
        int k, m = mdate.indexOf("-");
        if (m == -1) {
            return false;
        } else {
            s = mdate.substring(0, m);
            try {
                k = Integer.parseInt(s);
            } catch (NumberFormatException e) {
                return false;
            }
            if (k > 1900) {
                syear_int = GetYear(mdate);
            } else {
                return false;
            }
        }

        if (syear_int < 9999) {
            smon_int = GetMonth(mdate);

            if ((smon_int > 0) && (smon_int < 13)) {
                sday_int = GetDay(mdate);
                if ((sday_int > 0) && (sday_int < 32)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
        ȡ�õ������ڵ���

        @return int
        @roseuid 3DC2096C0098
     */
    public static int GetYear() {
        Date today = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy");
        String syear = sd.format(today);
        return Integer.parseInt(syear);
    }

    /**
        ��õ�ǰ����

        @return int
        @roseuid 3DC20AB8035D
     */
    public static int GetSecond() {
        Calendar now_Time = Calendar.getInstance();
        int second = now_Time.get(Calendar.SECOND);
        return second;
    }

    /**
        ��õ�ǰ��-���������

        @param year
        @param month
        @return int
        @roseuid 3DC206910246
     */


    public static int getMonMDay(int year, int month) {
        if ((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
            (month == 8) || (month == 10) || (month == 12)) {
            return 31;
        } else {
            if (month != 2) {
                return 30;
            } else {
                if (ifLeap(year)) {
                    return 29;
                } else {
                    return 28;
                }
            }
        }

    }

    /**
        ��������꣬����true ���򷵻�false

        @param year - ��������
        @return boolean
        @roseuid 3DC62FFC00C9
     */
    public static boolean ifLeap(int year) {
        if (!(year % 4 == 0)) {
            return false;
        } else {
            if (year % 100 != 0) {
                return true;
            } else {
                if (year % 400 == 0) {
                    return true;
                } else {
                    return false;
                }
            }
        }
    }

    /**
        ��õ�ǰ��-���������

        @param year
        @param month
        @return int
        @roseuid 3DC207160323
     */
    public static int getMonMDay(String year, String month) {
        int year_int, month_int;
        year_int = Integer.parseInt(year);
        month_int = Integer.parseInt(month);
        int d = getMonMDay(year_int, month_int);
        return d;
    }

    /**
        @param mdate
        @return String
        @since 2002-10-28
        @roseuid 3DC2204D014E
     */
    public String cnDate(String mdate) {
        String cndate, syear, smonth, sday;
        syear = String.valueOf(GetYear(mdate));
        smonth = String.valueOf(GetMonth(mdate));
        sday = String.valueOf(GetDay(mdate));
        cndate = syear + "��" + smonth + "��" + sday + "��";
        return cndate;
    }

    /**
        ��õ�ǰ��-���������

        @param year
        @param month
        @return int
        @roseuid 3DC20737038F
     */
    public static int getMonMDay(String year, int month) {
        int year_int = Integer.parseInt(year);
        int d = getMonMDay(year_int, month);
        return d;
    }

    /**
        ��õ�ǰ��-���������

        @param year
        @param month
        @return int
        @roseuid 3DC20759022F
     */
    public static int getMonMDay(int year, String month) {
        int month_int = Integer.parseInt(month);
        int d = getMonMDay(year, month_int);
        return d;
    }

    /**
        ��õ�ǰ�����ں�ʱ�� ��ʽ�硰2002-10-22 "

        @return String
        @roseuid 3DC20796034F
     */
    public static String now() {
        Date today = new Date();
        String snow, syear, smonth, sday;
        syear = String.valueOf(GetYear(today));
        smonth = String.valueOf(GetMonth(today));
        sday = String.valueOf(GetDay(today));
        if (smonth.length() < 2) {
            smonth = "0" + smonth;
        }
        if (sday.length() < 2) {
            sday = "0" + sday;
        }
        snow = syear + "-" + smonth + "-" + sday;
        return snow;
    }

    /**
        ��õ�ǰ�����ں�ʱ�� ��ʽ�硰2002-10-22 19:22:12��

        @return String
        @roseuid 3DC20796034F
     */
    public static String nowdate() {
        Date today = new Date();
        String snow, syear, smonth, sday, shour, sminute, ssecond;
        syear = String.valueOf(GetYear(today));
        smonth = String.valueOf(GetMonth(today));
        sday = String.valueOf(GetDay(today));
        shour = String.valueOf(GetHour());
        sminute = String.valueOf(GetMinute());
        ssecond = String.valueOf(GetSecond());
        if (smonth.length() < 2) {
            smonth = "0" + smonth;
        }
        if (sday.length() < 2) {
            sday = "0" + sday;
        }
        if (shour.length() < 2) {
            shour = "0" + shour;
        }
        if (sminute.length() < 2) {
            sminute = "0" + sminute;
        }
        if (ssecond.length() < 2) {
            ssecond = "0" + ssecond;
        }
        snow = syear + "-" + smonth + "-" + sday + " " + shour + ":" + sminute +
               ":" + ssecond;
        return snow;
    }


    public static String convertBigDate(String sdate) {
        String result = new String();
        result = "";
        if ((sdate.length() == 7)) { //2003-12
            String year = sdate.substring(0, 4);
            year = year.substring(2, 4);
            String month = sdate.substring(5, 7);
            String rs_month = new String();
            int i = 0;
            i = (int) Integer.parseInt(month);
            switch (i) {
            case 1: {
                rs_month = "Ҽ";
                break;
            }
            case 2: {
                rs_month = "��";
                break;
            }
            case 3: {
                rs_month = "��";
                break;
            }
            case 4: {
                rs_month = "��";
                break;
            }
            case 5: {
                rs_month = "��";
                break;
            }
            case 6: {
                rs_month = "½";
                break;
            }
            case 7: {
                rs_month = "��";
                break;
            }
            case 8: {
                rs_month = "��";
                break;
            }
            case 9: {
                rs_month = "��";
                break;
            }
            case 10: {
                rs_month = "ʰ";
                break;
            }
            case 11: {
                rs_month = "ʰҼ";
                break;
            }
            case 12: {
                rs_month = "ʰ��";
                break;
            }
            }
            result = year + "��" + rs_month + "��";
        }
        return result;
    }

    public static String getTime() {
        Date today = new Date();
        String snow, syear, smonth, sday, shour, sminute, ssecond;
        syear = String.valueOf(GetYear(today));
        smonth = String.valueOf(GetMonth(today));
        sday = String.valueOf(GetDay(today));
        shour = String.valueOf(GetHour());
        sminute = String.valueOf(GetMinute());
        ssecond = String.valueOf(GetSecond());
        if (smonth.length() < 2) {
            smonth = "0" + smonth;
        }
        if (sday.length() < 2) {
            sday = "0" + sday;
        }
        if (shour.length() < 2) {
            shour = "0" + shour;
        }
        if (sminute.length() < 2) {
            sminute = "0" + sminute;
        }
        if (ssecond.length() < 2) {
            ssecond = "0" + ssecond;
        }
        snow = syear + smonth + sday + shour + sminute + ssecond;
        return snow;
    }


    /**
        ȡ�õ������ڵ��·�  ��֤��}λ�ַ�

        @return String
        @roseuid 3DC209510284
     */
    public static String GetMonth_2() {
        Date today = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("MM");
        String smon = sd.format(today);
        return ((smon.length() == 1) ? ("0" + smon) : smon);
    }

    /**
        ȡ�õ������  ��֤��}λ�ַ�

        @return String
        @roseuid 3DC2091701F4
     */
    public static String GetDay_2() {
        Date today = new Date();
        SimpleDateFormat sd = new SimpleDateFormat("dd");
        String sday = sd.format(today);
        return ((sday.length() == 1) ? ("0" + sday) : sday);
    }

    public static String GetBackMonth(String ny) {
        String ny_ = "";
        String month = "";
        if (ny.length() == 7) {
            month = ny.substring(5, 7);
            if (month.substring(0, 1).equals("0")) {
                month = month.substring(1, 2);
            }
        } else {
            month = ny.substring(5, 6);
        }
        int int_month = Integer.parseInt(month);
        if (int_month != 1) {
            ny_ = ny.substring(0, 4) + "-" + Integer.toString(int_month - 1);
        } else {
            ny_ = Integer.toString(Integer.parseInt(ny.substring(0, 4)) - 1) +
                  "-12";
        }
        return ny_;
    }

    public static String getBackDay() {
        String dqsj = "";
        dqsj = pdate.now();
        String nd_dqsj = dqsj.substring(0, 4);
        String yf_dqsj = dqsj.substring(5, 7);
        String rq_dqsj = dqsj.substring(8, dqsj.length());
        int int_nd = Integer.parseInt(nd_dqsj);
        int int_yf = Integer.parseInt(yf_dqsj);
        int int_rq = Integer.parseInt(rq_dqsj);
        String backday = "";
        String backdayyear="";
        String backdaymonth="";
        String backdayday="";



        return backday;




    }
    /**
     * YYYY-MM-DD
     * @param date
     * @return
     */
    public static String getBackDay(String date) {
    	if (null != date && date.length() >= 10) {
	        String year = date.substring(0, 4);
	        String month = date.substring(5, 7);
	        String day = date.substring(8, 10);
	        if (day.equals("01")) {
				if (month.equals("01")) {
					year = String.valueOf(Integer.parseInt(year)-1);
					month = "12";
					day = "31";
				} else {
					month = String.valueOf(Integer.parseInt(month)-1);
					if (month.length() == 1) {
						month = "0"+month;
					}
					day = String.valueOf(getMonMDay(year,month));
				}
			} else {
				day = String.valueOf(Integer.parseInt(day)-1);
			}
	        if (day.length() == 1) {
	        	day = "0"+day;
	        }
	        date = year+"-"+month+"-"+day;
    	}
        return date;




    }
    //���ص�ǰ�����������µ��ϸ�����ĩ ��ǰ������Ϊ2006-09-25 eg:2006-08-31
    public static String getBackMonthEndDay(){
        String nd ="";
        String yf ="";
        String rq = "";
        String dqsj = "";
        //String dqsj = "";
        String backmonth="";
       dqsj = pdate.now();
       String dqny = dqsj.substring(0, 7);

       backmonth = pdate.GetBackMonth(dqny);

       int backmonthyear = Integer.parseInt(backmonth.substring(0,4));
       nd = String.valueOf(backmonthyear) ;
       int backmonthmonth = Integer.parseInt(backmonth.substring(5,backmonth.length()));

       if (String.valueOf(backmonthmonth).length()<2){
         yf = "0"+String.valueOf(backmonthmonth);
       }else{
        yf = String.valueOf(backmonthmonth);
       }

       int backday = pdate.getMonMDay(backmonthyear,backmonthmonth);

       if (String.valueOf(backday).length()<2){
        rq = "0"+String.valueOf(backday);
      }else{
       rq = String.valueOf(backday);
      }



       return nd+"-"+yf+ "-" + rq;






    }

}
